package com.liferay.jdbc;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;

import com.liferay.exception.MyException;
import com.mchange.v2.c3p0.ComboPooledDataSource;

public class DataSource {

    private static DataSource     datasource;
    private ComboPooledDataSource cpds;

    private DataSource() throws MyException{
        try {
        	cpds = new ComboPooledDataSource();
			cpds.setDriverClass("com.mysql.jdbc.Driver");
			cpds.setJdbcUrl("jdbc:mysql://localhost/test");
			cpds.setUser("root");
			cpds.setPassword("root");
			
			cpds.setMinPoolSize(5);
			cpds.setAcquireIncrement(5);
			cpds.setMaxPoolSize(20);
			cpds.setMaxStatements(180);
		} catch (PropertyVetoException e) {
			throw new MyException(e.getMessage());
		} 

    }

    public static DataSource getInstance() throws MyException {
        if (datasource == null) {
            try {
				datasource = new DataSource();
			} catch (MyException e) {
				throw new MyException(e.getMessage());
			}
            return datasource;
        } else {
            return datasource;
        }
    }

    public Connection getConnection() throws SQLException {
        return this.cpds.getConnection();
    }

}